import router from "./router";
import nprogress from "nprogress";
import 'nprogress/nprogress.css'
import store from "./store";

/**
 * 路由前置守卫
 */

const whiteList = ['/login', '/404']

router.beforeEach(async(to, from, next) => {

  nprogress.start()

  if(store.getters.token){
    if(to.path === '/login'){
      // 调到首页
      next('/');
      //  next 中有地址，不会走后置守卫
      nprogress.done()
    }else{
      // 直接放行
      next()
    }

  }else{
    if(whiteList.includes(to.path)){
      next()
    }else{
      next('/login')
      nprogress.done()
    }
  }
})

// 后置守卫
router.afterEach(() => {
  nprogress.done()
})